package com.android.nextBus.JSON;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.nextBus.DB.MapItem;
import com.google.android.maps.GeoPoint;

public class ShapeParser {
	
	public static MapItem parse(String data, int shapeID) {
		ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
		
		try {
			JSONArray jsonArray = new JSONArray(data);
			
			for (int i = 0; i < jsonArray.length(); ++i) {
				JSONObject obj = (JSONObject) jsonArray.get(i);
				
				int lon = obj.getInt("x");
				int lat = obj.getInt("y");
				
				GeoPoint p = new GeoPoint(lat, lon);
				
				points.add(p);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return new MapItem(shapeID, points);
	}
	
}
